package com.dormitoryAdministratorSystem.service.clazz.impl;

import com.dormitoryAdministratorSystem.dao.clazz.impl.ClassDaoImpl;
import com.dormitoryAdministratorSystem.entity.clazz.ClassBean;
import com.dormitoryAdministratorSystem.entity.page.PageBean;
import com.dormitoryAdministratorSystem.service.clazz.IClassService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ClassSerImpl implements IClassService {
    private ClassDaoImpl cdi = new ClassDaoImpl();

    @Override
    public List<ClassBean> getAllClasses() {
        return cdi.getAllClasses();
    }

    @Override
    public PageBean<ClassBean> showClassesInfo(Map<String, String> con) {
        PageBean<ClassBean> p = new PageBean<>();
        List<String> list = new ArrayList<>();
        String sql = "select cid,cname,openTime,teacher from t_class where 1 = 1 ";
        String countSql = "select count(*) as cNum from t_class where 1 = 1 ";

        if(!"".equals(con.get("cname")) && con.get("cname") != null){
            sql += " and cname like ? ";
            countSql += "and cname like ?";
            list.add("%"+con.get("cname")+"%");
        }

        if(!"".equals(con.get("teacher")) && con.get("teacher") != null){
            sql += " and teacher like ? ";
            countSql += " and teacher like ? ";
            list.add("%"+con.get("teacher")+"%");
        }

        if(!"".equals(con.get("sTime")) && con.get("sTime") != null){
            sql += " and openTime < ? ";
            countSql += " and openTime < ? ";
            list.add(con.get("sTime"));
        }

        if(!"".equals(con.get("eTime")) && con.get("eTime") != null){
            sql += " and openTime > ? ";
            countSql += " and openTime > ? ";
            list.add(con.get("eTime"));
        }

        if("".equals(con.get("curPage")) || con.get("curPage") == null){
            p.setCurPage(1);
        }else{
            p.setCurPage(Integer.parseInt(con.get("curPage")));
        }

        if("".equals(con.get("Size")) || con.get("Size") == null){
            p.setPageSize(5);
        }else{
            p.setPageSize(Integer.parseInt(con.get("Size")));
        }

        Object[] parms1 = new Object[list.size()];
        Object[] parms2 = new Object[parms1.length + 2];

        for (int i = 0; i < list.size(); i++) {
            parms1[i] = list.get(i);
            parms2[i] = list.get(i);
        }

        p.setTolNum(cdi.getClassTolNum(countSql,parms1));
        p.setTolSize((int) Math.ceil((double)p.getTolNum() / p.getPageSize()));

        sql += " order by cid desc limit ?, ?";
        parms2[parms1.length] = (p.getCurPage() - 1) * p.getPageSize();
        parms2[parms1.length + 1] = p.getPageSize();
        p.setDates(cdi.getClassByPage(sql,parms2));

        return p;
    }

    @Override
    public int addClass(ClassBean newClass) {
        return cdi.insertClass(newClass);
    }

    @Override
    public int deleClass(int cid) {
        return cdi.deleteClassById(cid);
    }
}
